package impl_v2.client;

import impl_v1.server.AuctioningSystemServer;

import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Scanner;

import model.AuctionPublic;
import model.exception.InvalidAuctionIDException;
import model.exception.InvalidPriceException;
import model.exception.UnavailableAuctionException;
import model.server.BuyerInterface2;

public class BuyerClient2 {

	public static void main(String[] args) {

		List<String> listServers = new ArrayList<String>();
		List<Integer> listAuctionsFollowed = new ArrayList<Integer>();

		System.out.println("Connect to server id :");
		Scanner scanner2 = new Scanner(System.in);
		String serverId = scanner2.nextLine();

		while (true) {
			try {
				System.out.println("Looking up server '" + serverId + "'");
				BuyerInterface2 auctioningSystem = (BuyerInterface2) Naming.lookup("rmi://localhost/AuctioningSystemService_"
						+ serverId);
				System.out.println("Connected to server '" + serverId + "'");

				listServers = auctioningSystem.getServerAddresses();
				System.out.print("List of all servers : ");
				for (String s : listServers) {
					System.out.print(s + ", ");
				}

				Map<Integer, AuctionPublic> list;

				boolean end = false;
				Scanner scanner = new Scanner(System.in);
				String choice;
				do {
					System.out.println();
					System.out.println("Select auction to bid on :");

					list = auctioningSystem.listAuctionsPublic();
					if (list.isEmpty()) {
						System.out.println("(No auction yet. Press Enter to refresh.)");
						scanner.nextLine();
						continue;
					}
					for (Integer id : list.keySet()) {
						System.out.println(id + " : " + list.get(id));
					}
					System.out.println("----------------------");
					System.out.println("Auctions you've bid on :");
					for (int id : listAuctionsFollowed) {
						AuctionPublic auction = auctioningSystem.getAuctionInfo(id);
						System.out.println(id + " : " + auction);
					}

					System.out.println("(Press Enter to refresh.)");
					choice = scanner.nextLine();
					try {
						int auctionChosen = Integer.parseInt(choice);

						System.out.println("Price (int) :");
						int price = scanner.nextInt();
						scanner.nextLine();

						System.out.println("Name :");
						String name = scanner.nextLine();

						System.out.println("Email :");
						String email = scanner.nextLine();

						try {
							auctioningSystem.bid(auctionChosen, price, name, email);
							listAuctionsFollowed.add(auctionChosen);
						} catch (InvalidAuctionIDException e) {
							System.out.println("This auction doesn't exist!");
							continue;
						} catch (UnavailableAuctionException e) {
							System.out.println("Sorry, this auction is closed.");
							continue;
						} catch (InvalidPriceException e) {
							System.out.println("Your price is too low!");
							continue;
						}

						System.out.println("Successful bid");

					} catch (NumberFormatException | InputMismatchException e) {
						continue;
					}
				} while (!end);

				scanner.close();

			} catch (MalformedURLException | RemoteException | NotBoundException e) {
				System.err.println(AuctioningSystemServer.SERVER_REMOTE_EXCEPTION);
				listServers.remove(serverId);
				Random randomGenerator = new Random();
				int index = 0;
				if (listServers.size() > 0) {
					index = randomGenerator.nextInt(listServers.size());
				} else {
					System.err.println(AuctioningSystemServer.NO_SERVERS_AVAILABLE_EXCEPTION);
					System.exit(1);
				}
				serverId = listServers.get(index);
			}
		}
	}
}
